﻿using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Repository.lmpl;
using RBAC.LuFang.Repository.lnterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace RBAC.LuFang.Domin.Services
{
    //菜单
    public class MenuService : IMenuService
    {
        private readonly IMenuRepository _menuRepository;
        //构造函数
        public MenuService(IMenuRepository menuRepository)
        {
            _menuRepository = menuRepository;
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task<int> AddMenu(MenuInfo menu)
        {
            try
            {
                var res = await _menuRepository.Get(x => x.menuInfoName == menu.menuInfoName && x.Pid == menu.Pid);
                if (res.Count() > 0)
                {
                    return -1;//该菜单已添加
                }
                menu.addName = "张三";
                menu.addDate = DateTime.Now;
                return await _menuRepository.GetAddAsync(menu);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示父级菜单
        /// </summary>
        /// <returns></returns>
        public async Task<List<MenuInfo>> GetMenuPid()
        {
            try
            {
                return await _menuRepository.GetAllAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示菜单
        /// </summary>
        /// <returns></returns>
        public async Task<Pagese> GetMenu(int pageindex, int pagesize, string? mname)
        {
            try
            {
                var me = await _menuRepository.GetAllAsync();
                Pagese page = new Pagese();
                
                var res = from menu in await _menuRepository.GetAllAsync()
                          select new GetMenuDTO
                          {
                              menuInfoId = menu.menuInfoId,
                              menuInfoName = menu.menuInfoName,
                              menuInfoUrl = menu.menuInfoUrl,
                              Pid = menu.Pid,
                              addName = menu.addName,
                              addDate = menu.addDate,
                              updName = menu.updName,
                              updDate = menu.updDate,
                              value = me.Where(x => x.menuInfoId == menu.Pid).Select(x => new MenuInfo
                              {
                                  menuInfoName=x.menuInfoName
                              }).ToList()
                          };
                if (!string.IsNullOrEmpty(mname))
                {
                    res = res.Where(x => x.menuInfoName.Contains(mname));
                }
                page.suncount = res.Count();
                page.lastpage = (int)Math.Ceiling((page.suncount * 1.0) / pagesize);
                page.list = res.OrderByDescending(x => x.addDate).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
                return page;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static List<MenuInfo> GetPid(List<MenuInfo> list,int id)
        {
            var result = list.Where(p => p.menuInfoId == id).ToList();//
            List<MenuInfo> children = new List<MenuInfo>(result);
            foreach (var item in result)
            {
                children.AddRange(GetPid(list, item.menuInfoId));
            }
            result.AddRange(children);
            return children;
        }
        /// <summary>
        /// 验证是否有子集
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        public async Task<int> Yan(int menuid)
        {
            try
            {
                var list = _menuRepository.GetAll().ToList();

                var delResult = GetDeleteIds(list, menuid);
                if (delResult.Count() > 0)
                {
                    return -1;//有子集
                }
                return 2;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        public async Task<int> MenuDel(int menuid)
        {
            try
            {
                var list =  _menuRepository.GetAll().ToList();//查询全部

                var delResult = GetDeleteIds(list, menuid);


                foreach (var delres in delResult)
                {
                    await _menuRepository.Del(delres);
                }
                var list1 = await _menuRepository.GetByidAsync(menuid);
               await  _menuRepository.Del(list1);

                return 1;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除调用递归
        /// </summary>
        /// <param name="list"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static List<MenuInfo> GetDeleteIds(List<MenuInfo> list, int menuId)
        {
            var result = list.Where(p => p.Pid == menuId).ToList();//要删除的所有子集
            List<MenuInfo> children =new List<MenuInfo>(result);
            foreach (var item in result)
            {
                children.AddRange(GetDeleteIds(list, item.menuInfoId));
            }
            result.AddRange(children);
            return children;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        public async Task<MenuInfo> GetByid(int menuid)
        {
            try
            {
                return await _menuRepository.GetByidAsync(menuid);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task<int> UpdMenu(MenuInfo menu)
        {
            try
            {
                menu.updName = "李四";
                menu.updDate = DateTime.Now;
                return await _menuRepository.GetUpd(menu);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
